<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 提现/兑换订单管理模块
 */
class Orders extends Admin_Controller {
	
    function __construct()
    {
        parent::__construct();
        $this->load->model('withdraw_order_model');
        $this->load->model('withdraw_list_model');
        $this->load->model('admin_log_model');
        $this->load->model('users_model');
        $this->load->model('account_model');
        $this->load->model('finance_list_model');
        $this->load->model('experience_list_model');
        $this->load->model('exchange_order_model');
        $this->load->model('exchange_list_model');
    }
    //首页-溜溜币实物商品
    public function index()
    {
        $where = array();
        if(isset($_GET['type']) && $_GET['type']){
            $where['withdraw_order.status'] = $_GET['type'];
            $this->template->assign('tt', $_GET['type']);
        }

        //变动类型
        if(isset($_GET['stm']) && $_GET['stm']){
            $where['withdraw_order.status'] = intval($_GET['stm']);
            $type = $_GET['stm'] == 1 ? '已支付' : ($_GET['stm'] == 2 ? '未支付' : '失败');
        }

        if(isset($_GET['q']) && $_GET['q']){
            $t = $_GET['st'];
            $t_title = $t == 1 ? '用户名' : '订单编号';
            $keyword = trim($_GET['q']);
            if($t == 1){
                $user = $this->users_model->get_user_by_username($keyword);
                if($user){
                    $where['withdraw_order.user_id'] = $user['user_id'];
                }
            }else{
                $where['order_nid'] = $keyword;
            }
        }

        if($_GET['time-start']){
            $where['create_time >='] = strtotime($_GET['time-start']." 00:00:00");
        }
        if($_GET['time-end']){
            $where['create_time <='] = strtotime($_GET['time-end']." 23:59:59");
        }
         //总数
        $total = $this->withdraw_order_model->get_count($where);
        $T_total = $this->withdraw_order_model->get_total_count($where);
        $navigation_position = 'outside';
        $records_per_page = $_GET['export'] ? 100000 : 10;
        $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position,
            array('left', 'right')) ? $navigation_position : 'outside');
        //$this->xpage->reverse(true);
        $this->xpage->records($total);
        $this->xpage->records_per_page($records_per_page);
        $this->xpage->labels("<<", ">>");
        //记录
        $list = $this->withdraw_order_model->get_list($where, (($this->xpage->get_page() - 1) * $records_per_page),
            $records_per_page);
        foreach ($list as $key=>$val){
            $where = array();
            $where['order_nid'] = $val['order_nid'];
            $order_ = $this->withdraw_list_model->get($where);
            $list[$key]['status'] = !$order_ ? 4 : $val['status'];
            $list[$key]['type'] = $val['type'] == 1 ? '支付宝' : '银行';
            $list[$key]['create_time'] = date('Y-m-d H:i:s',$val['create_time']);
            $list[$key]['update_time'] = $val['update_time'] ? date('Y-m-d H:i:s',$val['update_time']) : '--/--';
        }
        if($_GET['export']){
            $amount = 0;
            $fee = 0;
            $true_amount = 0;
            $this->load->library('Export');
            $ListRowData = array();
            foreach ($list as $key=>$val){
                 $temp = array();
                 $temp[] = $val['username'];
                 $temp[] = $val['order_nid'];
                 $temp[] = $val['amount'];
                 $temp[] = $val['fee'];
                 $temp[] = $val['true_amount'];
                 $temp[] = $val['name'];
                 $temp[] = $val['account'];
                 $temp[] = $val['type'];
                 $temp[] = $val['status'] == 1 ? '已支付' : ($val['status'] == 2 ? '未支付' : ($val['status'] == 3 ? '失败' : '订单异常' ) );
                 $temp[] = $val['create_time'];
                 $temp[] = $val['update_time'];
                 $temp[] = $val['remark'];
                 
                 $amount += $val['amount'];
                 $fee += $val['fee'];
                 if($val['status'] == 1){
                     $true_amount += $val['true_amount'];
                 }
                 $ListRowData[$key]['RowData'] = $temp;
             }
             //标题名称
            $temp['RowData'] = array();
            $temp['RowData'][] = "弹溜溜平台提现明细表".  date('Y-m-d');
            array_push($ListRowData,$temp);
            //导出时间
            $temp = array();
            $temp['RowData'][] = "起始时间：".($_GET['time-start'] ? $_GET['time-start'] : date('Y-m-d'));
            $temp['RowData'][] = "结束时间：".($_GET['time-end'] ? $_GET['time-end'] : date('Y-m-d'));
            array_push($ListRowData,$temp);
            //总支出
            $temp['RowData'] = array();
            $temp['RowData'][] = "用户总申请金额：".  sprintf('%0.2f',$amount).'元';
            $temp['RowData'][] = "用户总申请费用：".$fee.'元';
            $temp['RowData'][] = "平台总支出金额：".$true_amount.'元';
            array_push($ListRowData,$temp);
            $HeadData = array('用户名','订单编号','提现金额（元）','手续费（元）','实付金额（元）','提现账户名','提现账号','提现类型','状态','申请时间','处理时间','备注');
            $filename = '弹溜溜平台提现明细表'.date('Y-m-d');
            echo $this->export->users($HeadData,$ListRowData,$filename);exit;
        }
        $this->template->assign('name', $keyword);
        $this->template->assign('cont1', $type);
        $this->template->assign('cont', $t_title);
        $this->template->assign('keyword', $_GET['username']);
        $this->template->assign('T_total', $T_total);
        $this->template->assign('Total', $total);
        $this->template->assign('time_start', $_GET['time-start']);
        $this->template->assign('time_end', $_GET['time-end']);
        $this->template->assign('showpage', $this->xpage->render(true));
        $this->template->assign('list', $list);
        $this->template->load('orders/index');
    }
    //提现订单详情
    function o_detail(){
        $withdraw_award = $this->config_award['withdraw_award'];//
        $withdraw_award_degree = $this->config_award['withdraw_award_degree'];//会员等级奖励比例
        $mssage = array();
        if($_POST){
            $status = trim($_POST['status']);
            $status = $status == 1 ? 1 : 3;
            $remark = trim($_POST['remark']);
            $order_nid = trim($_POST['nid']);
            $item = $this->withdraw_order_model->get(array('order_nid'=>$order_nid,'withdraw_order.status'=>2));
            if(!$item){
                $mssage['info']	  = '找不到该订单或该订单已被处理过';
                $mssage['status'] = 0;
                $mssage['url']	  = site_url('index');
                echo json_encode($mssage);exit;
            }
            $cashValue = $item['amount'];
            $award  =$withdraw_award[$cashValue];
            //获取提现用户的信息
            $user = $this->users_model->get_user_by_uid($item['user_id']);
            if($status ==1){//同意支付
                $data = array();
                $data['status'] = 1;
                $data['remark'] = $remark;
                $data['update_time'] = time();
                $data['admin_name'] = $this->session->userdata('truename');
                $result = $this->withdraw_order_model->update($data,$order_nid);
                if($result){
                    //TODO短信通知
                    //支付通知
                    $message = "您于".date("Y-m-d H:i:s",$item['create_time']).'申请的'.$item['amount']."现金提现已经支付，支付时间".date("Y-m-d H:i:s",$data['update_time']).",请确认查收。感谢您对弹溜溜的支持！";
//                    $message = mail_notice_tpl($this->config_tpl['mail_tpl'], $message);
                    //消息类型配置
                    $notice_type_cf = $this->webconf['MSGTYPE'];
                    insert_award_notice(array('user_id'=>$item['user_id'],'title'=>'提现支付通知','content'=>$message,'from'=>'弹溜溜官方','create_time'=>  time()),$notice_type_cf['sms_type']);
                    
                    $order_list_data = array();
                    $order_list_data['status'] = $data['status'];
                    $order_list_data['update_time'] = $data['update_time'];
                    $order_list_data['remark'] = $data['remark'];
                    $order_list_data['admin_name'] = $data['admin_name'];
                    $this->withdraw_list_model->update($data,$order_nid);
                    
                    /*满多少元奖励*/
//                    $cashValue = $item['amount'];
//                    $award  =$withdraw_award[$cashValue];
//                    if($award){
//                        $finance = $award['finance'];
//                        $experience = $award['experience'];
//                        $msg_finance = "提现满".  intval($cashValue)."元奖励：".$finance."溜溜币";
//                        $msg_experience = "提现满".  intval($cashValue)."元奖励:".$experience."积分";
//                    }
//                    /*会员等级奖励*/
//                    $vip_degree = intval($user['vip_degree']);
//                    if($vip_degree){
//                        $vip_finance = $finance * $withdraw_award_degree[$vip_degree];
//                        $vip_experience = $experience * $withdraw_award_degree[$vip_degree];
//                        $msg_finance_vip = ",".$vip_degree."级会员额外奖励:".$vip_finance."溜溜币";
//                        $msg_experience_vip = ",".$vip_degree."级会员额外奖励:".$vip_experience."积分";
//                    }
//                    
//                    if($award){
//                        //获取用户账户信息
//                        $account = $this->account_model->get($user['user_id']);
//                        $account_data = array();
//                        $account_data['total'] = $account['total'] + $finance + ($vip_finance ? $vip_finance : 0);
//                        $account_data['balance'] = $account['balance'] + $finance + ($vip_finance ? $vip_finance : 0);
//                        $account_data['experience'] = $account['experience'] + $experience + ($vip_experience ? $vip_experience : 0);
//                        $account_data['experience_balance'] = $account['experience_balance'] + $experience + ($vip_experience ? $vip_experience : 0);
//                        $result = $this->account_model->update($account_data,$user['user_id']);
//                        if($result){
//                            //写入溜溜币明细
//                            $finance_list_data = array();
//                            $finance_list_data['user_id'] = $user['user_id'];
//                            $finance_list_data['content'] = $msg_finance.$msg_finance_vip;
//                            $finance_list_data['finance'] = $finance + ($vip_finance ? $vip_finance : 0);
//                            $finance_list_data['finance_balance'] = $account_data['balance'];
//                            $finance_list_data['reward_type'] = 'withdraw_award';
//                            $finance_list_data['type'] = 1;
//                            $finance_list_data['create_time'] = time();
//                            $finance_list_data['month'] = date("Ym");
//                            $finance_list_data['day'] = date("Ymd");
//                            $this->finance_list_model->insert($finance_list_data);
//                            //写入积分明细
//                            $experience_list_data = array();
//                            $experience_list_data['user_id'] = $user['user_id'];
//                            $experience_list_data['content'] = $msg_experience.$msg_experience_vip;
//                            $experience_list_data['experience'] = $experience + ($vip_experience ? $vip_experience : 0);
//                            $experience_list_data['experience_balance'] = $account_data['experience_balance'];
//                            $experience_list_data['reward_type'] = 'withdraw_award';
//                            $experience_list_data['type'] = 1;
//                            $experience_list_data['create_time'] = time();
//                            $experience_list_data['month'] = date("Ym");
//                            $experience_list_data['day'] = date("Ymd");
//                            $this->experience_list_model->insert($experience_list_data);
//                        }
//                        
//                    }
                    //管理员日志
                    $logdata = array();
                    $logdata['user_id'] = $this->userinfo['id'];
                    $logdata['username'] = $this->userinfo['username'];
                    $logdata['event'] = 'orders';
                    $logdata['content'] = '同意提现订单：'.$order_nid."的支付";
                    $this->admin_log_model->insert($logdata);
                    
                    $mssage['info']	  = '订单支付成功';
                    $mssage['status'] = 1;
                    $mssage['url']	  = site_url('orders/index');
                    echo json_encode($mssage);exit;
                }else{
                    $mssage['info']	  = '操作失败';
                    $mssage['status'] = 0;
                    $mssage['url']	  = site_url('orders/index');
                    echo json_encode($mssage);exit;
                }
                
            }else{//拒绝支付
                $data = array();
                $data['status'] = 3;
                $data['remark'] = $remark;
                $data['update_time'] = time();
                $data['admin_name'] = $this->session->userdata('truename');
                $result = $this->withdraw_order_model->update($data,$order_nid);
                if($result){
                    //支付通知
                    $message = "您于".date("Y-m-d H:i:s",$item['create_time']).'申请的'.$item['amount']."现金提现失败,原因：".$remark."。感谢您对弹溜溜的支持！";
//                    $message = mail_notice_tpl($this->config_tpl['mail_tpl'], $message);
                    $notice_type_cf = $this->webconf['MSGTYPE'];
                    insert_award_notice(array('user_id'=>$item['user_id'],'title'=>'提现支付通知','content'=>$message,'from'=>'弹溜溜官方','create_time'=>  time()), $notice_type_cf['sms_type']);
                    
                    $order_list_data = array();
                    $order_list_data['status'] = $data['status'];
                    $order_list_data['update_time'] = $data['update_time'];
                    $order_list_data['remark'] = $data['remark'];
                    $order_list_data['admin_name'] = $data['admin_name'];
                    $this->withdraw_list_model->update($data,$order_nid);
                    //管理员日志
                    $logdata = array();
                    $logdata['user_id'] = $this->userinfo['id'];
                    $logdata['username'] = $this->userinfo['username'];
                    $logdata['event'] = 'orders';
                    $logdata['content'] = '拒绝提现订单：'.$order_nid."的支付";
                    $this->admin_log_model->insert($logdata);
                    
                    $mssage['info']	  = '订单支付拒绝成功';
                    $mssage['status'] = 1;
                    $mssage['url']	  = site_url('orders/index');
                    echo json_encode($mssage);exit;
                }else{
                    $mssage['info']	  = '操作失败';
                    $mssage['status'] = 0;
                    $mssage['url']	  = site_url('orders/index');
                    echo json_encode($mssage);exit;
                }
            }
            
        }
        $order_nid = isset($_GET['nid']) && $_GET['nid'] ? trim($_GET['nid']) : '';
        $item = $this->withdraw_order_model->get(array('order_nid'=>$order_nid));
        if(!$item) redirect ('orders');
        $item['type'] = $item['type'] == 1 ? '支付宝' : '银行卡';
        $item['create_time'] = date('Y-m-d H:i:s',$item['create_time']);
        $item['update_time'] = $item['update_time'] ? date('Y-m-d H:i:s',$item['update_time']) : '--/--';
        $this->template->assign('item', $item);
        $this->template->assign('segment_p','index');
        $this->template->load('orders/o_detail');
    }
    
    function exchange(){
        
        $where = array();
        if(isset($_GET['type']) && $_GET['type']){
            $where['exchange_order.status'] = $_GET['type'];
            $this->template->assign('tt', $_GET['type']);
        }

        //变动类型
        if(isset($_GET['stm']) && $_GET['stm']){
            $where['exchange_order.status'] = intval($_GET['stm']);
            $type = $_GET['stm'] == 1 ? '已兑换' : ($_GET['stm'] == 2 ? '申请中' : '失败');
        }

        if(isset($_GET['q']) && $_GET['q']){
            $t = $_GET['st'];
            $t_title = $t == 1 ? '用户名' : ($t == 2 ? "订单编号" : "快递单号");
            $keyword = trim($_GET['q']);
            if($t == 1){
                $user = $this->users_model->get_user_by_username($keyword);
                if($user){
                    $where['exchange_order.user_id'] = $user['user_id'];
                }
            }elseif($t == 2){
                $where['order_nid'] = $keyword;
            }else{
                $where['express_number'] = $keyword;
            }
        }

        if($_GET['time-start']){
            $where['create_time >='] = strtotime($_GET['time-start']);
        }
        if($_GET['time-end']){
            $where['create_time <='] = strtotime($_GET['time-end']);
        }
        
         //总数
        $total = $this->exchange_order_model->get_count($where);
        $T_total = $this->exchange_order_model->get_total_count($where);
        $navigation_position = 'outside';
        $records_per_page = $_GET['export'] ? 100000 : 10;
        $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position,
            array('left', 'right')) ? $navigation_position : 'outside');
        //$this->xpage->reverse(true);
        $this->xpage->records($total);
        $this->xpage->records_per_page($records_per_page);
        $this->xpage->labels("<<", ">>");
        //记录
        $list = $this->exchange_order_model->get_list($where, (($this->xpage->get_page() - 1) * $records_per_page),
            $records_per_page);
        foreach ($list as $key=>$val){
            $where = array();
            $where['order_nid'] = $val['order_nid'];
            $order_ = $this->exchange_list_model->get($where);
            $list[$key]['amount'] = number_format($val['amount']);
            $list[$key]['true_amount'] = number_format($val['amount']/10000);
            $list[$key]['status'] = !$order_ ? 4 : $val['status'];
            $list[$key]['type'] = $val['type'] == 1 ? '实物' : '虚拟';
            $list[$key]['create_time'] = date('Y-m-d H:i:s',$val['create_time']);
            $list[$key]['update_time'] = $val['update_time'] ? date('Y-m-d H:i:s',$val['update_time']) : '--/--';
        }
        
        if($_GET['export']){
            $this->load->library('Export');
            $ListRowData = array();
            foreach ($list as $key=>$val){
                 $temp = array();
                 $temp[] = $val['username'];
                 $temp[] = $val['order_nid'];
                 $temp[] = $val['title'];
                 $temp[] = $val['amount'];
                 $temp[] = $val['true_amount'];
                 $temp[] = $val['type'];
                 $temp[] = $val['status'] == 1 ? '已兑换' : '失败';
                 $temp[] = $val['account'];
                 $temp[] = $val['name'];
                 $temp[] = $val['phone'];
                 $temp[] = $val['address'];//
                 $temp[] = $val['express_number'];
                 $temp[] = $val['create_time'];
                 $temp[] = $val['update_time'];
                 $temp[] = $val['remark'];
                 $ListRowData[$key]['RowData'] = $temp;
             }
             $HeadData = array('用户名','订单编号','产品名称','所需溜溜币','所需金额（元）','商品类型','兑换状态','充值账号','兑换人姓名（实物兑换）','兑换人手机（实物兑换）','兑换人地址（实物兑换）','快递单号（实物兑换）','申请时间','处理时间','备注');
             $filename = '兑换订单表（'.date('Y-m-d').")";
             echo $this->export->users($HeadData,$ListRowData,$filename);
        }
        
        $this->template->assign('name', $keyword);
        $this->template->assign('cont1', $type);
        $this->template->assign('cont', $t_title);
        $this->template->assign('mark', $t_title);
        $this->template->assign('keyword', $_GET['username']);
        $this->template->assign('T_total', $T_total);
        $this->template->assign('Total', $total);
        $this->template->assign('time_start', $_GET['time-start']);
        $this->template->assign('time_end', $_GET['time-end']);
        $this->template->assign('showpage', $this->xpage->render(true));
        $this->template->assign('list', $list);
        $this->template->load('orders/exchange');
    }
    //兑换订单详情页
    function e_detail(){
        if($_POST){
            $status = trim($_POST['status']);
            $express_num = trim($_POST['number']);
            $order_nid = trim($_POST['nid']);
            $remark = trim($_POST['remark']);
            $item = $this->exchange_order_model->get(array('order_nid'=>$order_nid,'exchange_order.status'=>2));
            if(!$item){
                $mssage['info']	  = '找不到该订单或该订单已被处理过';
                $mssage['status'] = 0;
                $mssage['url']	  = site_url('index');
                echo json_encode($mssage);exit;
            }
            
            //同意兑换
            if($status == 1){
                $users = $this->users_model->get_user_by_uid($item['user_id']);
                //兑换订单信息
                $order_data = array();
                $order_data['express_number'] = $express_num;
                $order_data['status'] = 1;
                $order_data['update_time'] = time();
                $order_data['admin_name'] = $this->session->userdata('truename');
                $order_data['remark'] = $remark;//兑换商品备注
                $result = $this->exchange_order_model->update($order_data,$order_nid);
                if($result){
                    //TODO短信通知
                    //兑换通知
                    if($item['type'] == 1){
                        $message = "您于".date("Y-m-d H:i:s",$item['create_time'])."申请兑换的商品【".$item['title']."】已发货，".$remark."，请注意查收。感谢您对弹溜溜的支持！";
                    }else{
                        $message = "您于".date("Y-m-d H:i:s",$item['create_time'])."申请兑换的商品【".$item['title']."】已兑换成功，请注意查收。感谢您对弹溜溜的支持！";
                    }
                    
                    $message = mail_notice_tpl($this->config_tpl['mail_tpl'], $message);
                    $this->db->insert("users_award_notice", array('user_id'=>$item['user_id'],'title'=>'兑换通知','content'=>$message,'from'=>'弹溜溜官方','create_time'=>  time()));
                    $order_list_data = array();
                    $order_list_data['status'] = $order_data['status'];
                    $order_list_data['update_time'] = $order_data['update_time'];
                    $order_list_data['remark'] = $order_data['remark'];
                    $order_list_data['admin_name'] = $order_data['admin_name'];
                    $this->exchange_list_model->update($order_list_data,$order_nid);
                    
                    //管理员日志
                    $logdata = array();
                    $logdata['user_id'] = $this->userinfo['id'];
                    $logdata['username'] = $this->userinfo['username'];
                    $logdata['event'] = 'orders';
                    $logdata['content'] = '同意兑换订单：'.$order_nid."的支付";
                    $this->admin_log_model->insert($logdata);
                    
                    $mssage['info']	  = '订单支付成功';
                    $mssage['status'] = 1;
                    $mssage['url']	  = site_url('orders/exchange');
                    echo json_encode($mssage);exit;
                }else{
                    $mssage['info']	  = '操作失败';
                    $mssage['status'] = 0;
                    $mssage['url']	  = site_url('orders/exchange');
                    echo json_encode($mssage);exit;
                }
            }else{//拒绝兑换
                $data = array();
                $data['status'] = 3;
                $data['remark'] = $remark;
                $data['update_time'] = time();
                $data['admin_name'] = $this->session->userdata('truename');
                $result = $this->exchange_order_model->update($data,$order_nid);
                if($result){
                    
                    //获取用户账户信息
                    $account = $this->account_model->get($item['user_id']);
                    $account_data = array();
                    
                    $account_data['balance'] = $account['balance'] + $item['amount'];//账户余额
                    $account_data['frost'] = $account['frost']-$item['amount'];//冻结金额
                    //更新账户
                    $this->account_model->update($account_data,$item['user_id']);

                    //弹溜溜明细
                    $finance_list_data = array();
                    $finance_list_data['user_id'] = $item['user_id'];
                    $finance_list_data['content'] = '兑换【'.$item['title'].'】失败，返回兑换金额';
                    $finance_list_data['finance'] = $item['amount'];
                    $finance_list_data['finance_balance'] = $account_data['balance'];
                    $finance_list_data['reward_type'] = 'other_award';
                    $finance_list_data['type'] = 1;//收入
                    $finance_list_data['create_time'] = time();
                    $finance_list_data['month'] = date("Ym");
                    $finance_list_data['day'] = date("Ymd");
                    $this->finance_list_model->insert($finance_list_data);
                    
                    //兑换通知
                    $message = "您于".date("Y-m-d H:i:s",$item['create_time'])."申请的【".$item['title']."】兑换失败，原因：<span style='color:red'>".$remark."</span>。订单已失效，您所消耗的<span class='mail-award'>".$item['amount']."溜溜币</span>全额返回，请注意查收。<br/><span class='mail-notice'>弹溜溜官方提醒您，兑换商品时请务必填写真实信息！</span>感谢您对弹溜溜的支持！";
                    $message = mail_notice_tpl($this->config_tpl['mail_tpl'], $message);
                    $this->db->insert("users_award_notice", array('user_id'=>$item['user_id'],'title'=>'兑换通知','content'=>$message,'from'=>'弹溜溜官方','create_time'=>  time()));
                    
                    $order_list_data = array();
                    $order_list_data['status'] = $data['status'];
                    $order_list_data['update_time'] = $data['update_time'];
                    $order_list_data['remark'] = $data['remark'];
                    $order_list_data['admin_name'] = $data['admin_name'];
                    $this->exchange_list_model->update($data,$order_nid);
                    //管理员日志
                    $logdata = array();
                    $logdata['user_id'] = $this->userinfo['id'];
                    $logdata['username'] = $this->userinfo['username'];
                    $logdata['event'] = 'orders';
                    $logdata['content'] = '拒绝兑换订单：'.$order_nid."的兑换";
                    $this->admin_log_model->insert($logdata);
                    
                    $mssage['info']	  = '订单兑换拒绝成功';
                    $mssage['status'] = 1;
                    $mssage['url']	  = site_url('orders/exchange');
                    echo json_encode($mssage);exit;
                }else{
                    $mssage['info']	  = '操作失败';
                    $mssage['status'] = 0;
                    $mssage['url']	  = site_url('orders/exchange');
                    echo json_encode($mssage);exit;
                }
            }
        }
        $order_nid = isset($_GET['nid']) && $_GET['nid'] ? trim($_GET['nid']) : '';
        $item = $this->exchange_order_model->get(array('order_nid'=>$order_nid));
        if(!$item) redirect ('orders/exchange');
        $item['amount'] = number_format($item['amount']);
        $item['create_time'] = date('Y-m-d H:i:s',$item['create_time']);
        $item['update_time'] = $item['update_time'] ? date('Y-m-d H:i:s',$item['update_time']) : '--/--';
        $this->template->assign('item', $item);
        $this->template->assign('segment_p','exchange');
        $this->template->load('orders/e_detail');
    }
    
    //充值订单
    function recharges(){
        $where = " 1 = 1";
        $where1 = " 1 = 1";
        if(isset($_GET['type']) && $_GET['type']){
            if($_GET['type'] == 1){//已支付
                $where .= " and o.status = 1";
            }else{//未支付
                $where .= " and o.status = 0";
            }
            
            $this->template->assign('st', $_GET['type']);
        }
        
        if($_GET['time-start']){
            $where .= " and o.create_time >= ".strtotime($_GET['time-start']);
            $where1 .= " and o.create_time >= ".strtotime($_GET['time-start']);
        }
        if($_GET['time-end']){
            $where .= " and o.create_time <= ".strtotime($_GET['time-end']." 23:59:59");
            $where1 .= " and o.create_time <= ".strtotime($_GET['time-end']." 23:59:59");
        }
        
        
        if(isset($_GET['q']) && $_GET['q']){
            $t = $_GET['st'];
            $t_title = $t == 1 ? '用户名' : "订单编号";
            $keyword = trim($_GET['q']);
            if($t == 1){
                $where .= " and u.username = '$keyword'";
                $where1 .= " and u.username = '$keyword'";
            }elseif($t == 2){
                $where .= " and o.order_nid = '$keyword'";
                $where1 .= " and o.order_nid = '$keyword'";
            }
        }
        
         //总数
        $total = $this->db->query("select o.* from tan_bbs_recharge_order o left join tan_users u on u.user_id = o.user_id where $where")->num_rows();
        $navigation_position = 'outside';
        $records_per_page = 10;
        $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position,
            array('left', 'right')) ? $navigation_position : 'outside');
        //$this->xpage->reverse(true);
        $this->xpage->records($total);
        $this->xpage->records_per_page($records_per_page);
        $this->xpage->labels("<<", ">>");
        //记录
        $list = $this->db->query("select o.*,u.username from tan_bbs_recharge_order o left join tan_users u on u.user_id = o.user_id where $where order by o.create_time desc limit ".(($this->xpage->get_page() - 1) * $records_per_page).",".$records_per_page)->result_array();
        foreach ($list as $key=>$val){
            $list[$key]['create_time'] = date("Y-m-d H:i:s",$val['create_time']);
            $list[$key]['update_time'] = $val['update_time'] ? date("Y-m-d H:i:s",$val['update_time']) : "--/--";
            $list[$key]['recharge_type'] = $val['recharge_type'] == "alipay" ? "支付宝" : "其他";
        }
        
        //已支付金额
        $recharge_sucess = $this->db->query("select SUM(o.product_money) product_money from tan_bbs_recharge_order o left join tan_users u on u.user_id = o.user_id where $where1 and o.status = 1 order by o.create_time desc")->row_array();
        if(isset($_GET['type']) && $_GET['type']){
            if($_GET['type'] == 1){//已支付
                $where1 .= " and o.status = 1";
            }else{//未支付
                $where1 .= " and o.status = 0";
            }
        }
        //总充值金额
        $recharge_total = $this->db->query("select SUM(o.product_money) product_money from tan_bbs_recharge_order o left join tan_users u on u.user_id = o.user_id where $where1 order by o.create_time desc")->row_array();
        
        $this->template->assign('recharge_sucess', $recharge_sucess['product_money']);
        $this->template->assign('recharge_total', $recharge_total['product_money']);
        $this->template->assign('name', $keyword);
        $this->template->assign('cont', $t_title);
        $this->template->assign('usermark', $t);
        $this->template->assign('time_start', $_GET['time-start']);
        $this->template->assign('time_end', $_GET['time-end']);
        $this->template->assign('showpage', $this->xpage->render(true));
        $this->template->assign('list', $list);
        $this->template->assign('Total', $total);
        $this->template->load('orders/recharges');
    }
}
